<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
		<title>e-Library Search Page</title>
		<link href="style/style.css" rel="stylesheet" type="text/css" />
	</head>

	<body>

		<div class="container">

			<div class="header">
				<div class="headertext"></div>
				<!--div class="headerbooks"></div-->
			</div>
		
			<!--div class="ml"></div-->
				<ul class="navlist">
					<!--li class="navlist_home"></li-->
					<li class="blank">Browse by:</li>
					<li class="navlist">Title</li>
					<li class="navlist">Author</li>
					<li class="navlist">Subject</li>
				</ul>
			<!--div class="mr"></div-->

			<div class="content">

			<?php

				// Establish a connection to the database
				mysql_connect("127.0.0.1", "root", "root");
				
				// Select the database to use
				mysql_select_db("library");

				// Get the search type
				$type = $_REQUEST["searchType"];
				
				// Other variables
				$continue = 1;
				$result = "";
				$counter = 0;

				// The user is searching by Author
				if ($type == "Author")
				{
					// Get the firstname, lastname and initial of the author
					$firstName = $_REQUEST["firstName"];
					$lastName = $_REQUEST["lastName"];
					$initial = $_REQUEST["initial"];
					
					// Blank string (unused)
					$authorString = "";
					
					// The search term
					$termStr = "";
			
					// Add the 3 words together to form the search term (to display to the user)
					if (strlen($firstName) > 0)
						$termStr .= $firstName . " ";
					if (strlen($initial) > 0)
						$termStr .= $initial . ". ";
					if (strlen($lastName) > 0)
						$termStr .= $lastName;
		
					// Display the search type and term to the user (break PHP)
					?>
					
					<br />
					<br />
					
					<table>
						<tr>
							<td><strong>Search type</strong></td>
							<td>Author</td>
						</tr>
						<tr>
							<td><strong>Search term</strong></td>
							<td><?php echo $termStr; ?></td>
						</tr>
						<tr>
							<td><strong>Search results</strong></td>
						</tr>
					</table>
					
					<br />
					<br />
					<br />
					<br />
					
					<table border = '1'>
						<tr>
							<th>Call No</th>
							<th>Author</th>
							<th>Title</th>
						</tr>
						
							<?php
							
							// Perform the query
							$q1 = mysql_query("SELECT b.CallNo, a.FirstName, a.Initial, a.LastName, b.Title FROM AUTHOR a, BOOK b, BOOKAUTHOR ba WHERE a.FirstName LIKE '%" . $firstName . "%' AND a.Initial LIKE '%" . $initial . "%' AND a.LastName LIKE '%" . $lastName . "%' AND ba.AuthorID = a.AuthorID AND b.BookId = ba.BookID");

							while ($authorRow = mysql_fetch_assoc($q1))
							{
								if ($counter == 0)
									$tempStr = $authorRow["LastName"] . ", " . $authorRow["FirstName"] . " " . $authorRow["Initial"] . ".";
								else
									$tempStr = " & " . $authorRow["LastName"] . ", " . $authorRow["FirstName"] . " " . $authorRow["Initial"] . ".";
								$authorString .= $tempStr;
								$counter++;
							}
							echo "<tr><td>" . $authorRow["CallNo"] . "</td><td>" . $authorString . "</td><td>" . $authorRow["Title"] . "</td></tr>";
							$counter = 0;
							$authorString = "";
							
							/* End Author Search */
						}
						else if ($type == "Title")
						{
							/* Performing a Title Search */
							
							// Get the search query (title to search for)
							$input = $_REQUEST["searchQuery"];
							
							// Display the search table to the user (braek PHP)
							?>
							
							<table cellspacing = '2'>
								<tr>
									<td><strong>Search type</strong></td>
									<td>Title</td>
								</tr>
								<tr>
									<td><strong>Search term</strong></td>
									<td><?php echo $input; ?></td></tr>
								<tr>
									<td><strong>Search results</strong></td>
								</tr>
							</table>
							
							<table cellpadding = '2' cellspacing = '1' border = '1'>
								<tr>
									<th>Call No</th>
									<th>Author</th>
									<th>Title</th>
								</tr>
								
							<?php
							
							// Back in PHP, perform the query
							$q1 = mysql_query("SELECT b.CallNo, b.Title, a.AuthorName FROM BOOK b, AUTHOR a WHERE (Title LIKE '%" . $input . "%') AND (b.AuthorID = a.AuthorID)");

							// Loop through the results of the query
							while ($titleRow = mysql_fetch_assoc($q1))
							{
								// Display the results (break PHP)
								?>
								
								<tr>
									<td><?php echo $titleRow["CallNo"]; ?></td>
									<td><?php echo $titleRow["AuthorName"]; ?></td>
									<td><?php echo $titleRow["Title"]; ?></td>
								</tr>
								
								<?php
							}
							
							// Close the table
							echo "</table>";
						}
						else if ($type == "Subject")
						{
							// Get the query to search for
							$input = $_REQUEST["searchQuery"];
							$counter == 0;
							$authorString = "";

							echo "<table cellspacing = '2'><tr><td><strong>Search type</strong></td><td>Title</td></tr><tr><td><strong>Search term</strong></td><td>" . $input . "</td></tr><tr><td><strong>Search results</strong></td></tr></table>";
							echo "<table cellpadding = '1' cellspacing = '1' border = '1'><tr><th>Call No</th><th>Author</th><th>Title</th><th>Subject</th></tr>";
							$q1 = mysql_query("SELECT * FROM SUBJECTS WHERE SubjectDesc LIKE '%" . $input . "%'");

							while ($subjectRow = mysql_fetch_assoc($q1))
							{
								$q2 = mysql_query("SELECT * FROM BOOKSUBJECTS WHERE SubjectID = " . $subjectRow["SubjectID"]);

								while ($bookSubjectsRow = mysql_fetch_assoc($q2))
								{
									$q3 = mysql_query("SELECT a.FirstName, a.Initial, a.LastName FROM AUTHOR a, BOOKAUTHOR ba, BOOKSUBJECTS bs WHERE a.AuthorID = ba.AuthorID AND ba.BookID = bs.BookID AND bs.SubjectID = " . $bookSubjectsRow["SubjectID"]);
				
									$counter = 0;

									while ($authorData = mysql_fetch_assoc($q3))
									{
										$q4 = mysql_query("SELECT * FROM BOOK WHERE BookID = '" . $bookSubjectsRow["BookID"] . "'");

										$bookData = mysql_fetch_assoc($q4);
					
										if ($counter == 0)
											$tempStr = $authorData["LastName"] . ", " . $authorData["FirstName"] . " " . $authorData["Initial"] . ".";
										else
											$tempStr = " & " . $authorData["LastName"] . ", " . $authorData["FirstName"] . " " . $authorData["Initial"] . ".";
										$authorString .= $tempStr;
										$counter++;
									}
								}
								echo "<tr><td>" . $bookData["CallNo"] . "</td><td>" . $authorString . "</td><td>" . $bookData["Title"] . "</td><td>" . $subjectRow["SubjectDesc"] . "</td></tr>";
								$authorString = "";
							}
						}
						else
						{
							echo "Search error";
							$continue = 0;
						}
					?>

			</div>
		</div>

		<!--div class="clear"></div-->
		<div class="footer">
			<hr />
			<p>&copy; 2009 - Website created by Craig, Scott and Stephen <!--| <a href="">Contact Us</a>--></p>
		</div>

	</body>
</html>
<?php

// Close the MySQL Database connection
mysql_close();

?>